<template>
	<div :key="uploadKey" class="padding">
		<div class="f-between">
			<h3>养老机构综合运营补贴自查报告</h3>
			<el-button text v-if="!isDetail" type="primary" @click="toDownload">下载模板</el-button>
		</div>
		<div class="mt12" v-if="!isDetail">
			<AplidUpload
				v-model="modelData.imageList"
				listType="picture-card"
				:showFileList="false"
				:fileType="uploadType.fileMetaType"
				name="自查报告"
        uploadTip="上传自查报告"
				:limit="1"
				:multiple="false"
				:disabled="isDetail"
				@change="uploadChange"
			></AplidUpload>
		</div>
		<div style="width: 150px" v-else>
			<FileList :src="modelData.imageList" :only-ready="true" :showFileList="true"> </FileList>
		</div>
	</div>
</template>

<script setup lang="ts">
import AplidUpload from '@/components/AplidCrud/src/AplidUpload/index.vue'
import FileSaver from 'file-saver'
import apiMod from '@/apiMod/index'
import FileList from '@/components/AplidCrud/src/AplidUpload/PreviewImg/index.vue'
import { uploadType } from '@/utils/util'

const props = defineProps<{
	params?: any
	creditCode?: string | number
	editData?: any
	isDetail?: boolean
	caseId?: string | number
	custome?: boolean
	approvalStatus?: string | number
}>()
let modelData = reactive({ imageList: '' })

// 去下载模板
const toDownload = () => {
	FileSaver.saveAs('https://aplid-51.oss-cn-hangzhou.aliyuncs.com/1718698175312_beecb6d3.docx', '自查报告.docx')
}

let newCaseId = ref('')
const addSave = async () => {
	if(props.params&&props.params.belongDate){
		let date = props.params.belongDate.split('-')
		let params0 = {
			applyYear: date[0],
			applyMonth: date[1],
			dataContent: { imageList: modelData.imageList },
			dataResults: { imageList: modelData.imageList },
			fromId: props.editData.formId,
			fromType: 'report_1002',
			tableTemplateId: 'report_1002',
			projectId: props.params.subsidyProjectId,
		}
		let res = await apiMod.view_formResults_add(params0)
		newCaseId.value = res.data
	}
}

const eidtSave = () => {
	let query = {
		id: props.editData.result_case_id || newCaseId.value,
		dataContent: { imageList: modelData.imageList },
		dataResults: { imageList: modelData.imageList },
		fromId: props.editData.formId,
		fromType: 'report_1002',
		tableTemplateId: 'report_1002',
		projectId: props.params.subsidyProjectId,
	}
	apiMod.view_formResults_edit(query).then((res) => {
		ElMessage.success('修改成功')
	})
}
const uploadChange = (data) => {
	eidtSave()
}
let uploadKey = ref(0)
onMounted(() => {
	// 没有id就进行第一次保存
	if (!props.editData.result_case_id) {
		addSave()
	}
	modelData = props.editData
	uploadKey.value = Math.random()
})
</script>
<style scoped lang="scss">
:deep(.el-upload--picture-card) {
  width: 100%;
  height: 100%
}

</style>
